package com.ruoyi.business.domain.enums;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单类型枚举
 * 区分不同业务场景的订单类型
 */
public enum OrderType {

    /**
     * 普通商城 - 纯金额支付
     */
    NORMAL(0, "普通商城", "普通商城订单，纯金额支付"),

    /**
     * 纯积分 - 积分商城：完全使用积分支付
     */
    POINTS_ONLY(1, "纯积分", "积分商城：完全使用积分支付"),

    /**
     * 积分+金额 - 积分商城：积分+金额混合支付
     */
    POINTS_AND_MONEY(2, "积分+金额", "积分商城：积分+金额混合支付"),

    /**
     * KOC产品兑换 - KOC专属产品兑换
     */
    KOC_EXCHANGE(3, "KOC产品兑换卷", "KOC专属产品兑换");

    private final Integer code;
    private final String name;
    private final String description;

    OrderType(Integer code, String name, String description) {
        this.code = code;
        this.name = name;
        this.description = description;
    }

    /**
     * 根据code获取枚举
     */
    public static OrderType getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (OrderType type : values()) {
            if (type.getCode().equals(code)) {
                return type;
            }
        }
        throw new IllegalArgumentException("未知的订单类型码: " + code);
    }

    /**
     * 根据name获取枚举
     */
    public static OrderType getByName(String name) {
        if (name == null || name.trim().isEmpty()) {
            return null;
        }
        for (OrderType type : values()) {
            if (type.getName().equals(name.trim())) {
                return type;
            }
        }
        throw new IllegalArgumentException("未知的订单类型名称: " + name);
    }

    /**
     * 检查是否为积分商城订单
     */
    public boolean isPointsMallOrder() {
        return this == POINTS_ONLY ||
            this == POINTS_AND_MONEY;
    }

    /**
     * 检查是否为普通商城订单
     */
    public boolean isNormalMallOrder() {
        return this == NORMAL;
    }

    /**
     * 检查是否为KOC订单
     */
    public boolean isKocOrder() {
        return this == KOC_EXCHANGE;
    }

    /**
     * 检查是否需要积分
     */
    public boolean requiresPoints() {
        return this == POINTS_ONLY ||
            this == POINTS_AND_MONEY ||
            this == KOC_EXCHANGE;
    }

    /**
     * 检查是否需要金额支付
     */
    public boolean requiresMoney() {
        return this == NORMAL ||
            this == POINTS_AND_MONEY;
    }

    /**
     * 检查是否需要审批流程（积分商城特有）
     */
    public boolean requiresApproval() {
        return isPointsMallOrder();
    }

    /**
     * 获取支持的支付方式
     */
    public List<String> getSupportedPaymentMethods() {
        List<String> methods = new ArrayList<>();

        switch (this) {
            case NORMAL:
                methods.add("ALIPAY");
                methods.add("WECHAT_PAY");
                methods.add("BANK_CARD");
                break;
            case POINTS_ONLY:
                methods.add("POINTS");
                break;
            case POINTS_AND_MONEY:
                methods.add("POINTS");
                methods.add("ALIPAY");
                methods.add("WECHAT_PAY");
                break;
            case KOC_EXCHANGE:
                methods.add("POINTS");
                methods.add("KOC_COUPON");
                break;
        }

        return methods;
    }

    /**
     * 获取订单类型对应的状态流程
     */
    public List<OrderStatus> getStatusWorkflow() {
        List<OrderStatus> workflow = new ArrayList<>();

        switch (this) {
            case NORMAL:
                workflow.add(OrderStatus.WAITING_PAYMENT);
                workflow.add(OrderStatus.PAID);
                workflow.add(OrderStatus.SHIPPED);
                workflow.add(OrderStatus.COMPLETED);
                break;
            case POINTS_ONLY:
            case POINTS_AND_MONEY:
                workflow.add(OrderStatus.WAITING_PAYMENT);
                workflow.add(OrderStatus.PAID);
                workflow.add(OrderStatus.PAYMENT_APPROVAL_PENDING);
                workflow.add(OrderStatus.APPROVED_PENDING_SHIPMENT);
                workflow.add(OrderStatus.SHIPPED);
                workflow.add(OrderStatus.COMPLETED);
                break;
            case KOC_EXCHANGE:
                workflow.add(OrderStatus.WAITING_PAYMENT);
                workflow.add(OrderStatus.PAID);
                workflow.add(OrderStatus.SHIPPED);
                workflow.add(OrderStatus.COMPLETED);
                break;
        }

        return workflow;
    }

    /**
     * 检查订单类型是否支持某个状态
     */
    public boolean supportsStatus(OrderStatus status) {
        return getStatusWorkflow().contains(status);
    }

    /**
     * 获取默认超时时间（分钟）
     */
    public Integer getDefaultTimeoutMinutes() {
        switch (this) {
            case NORMAL:
                return 30; // 普通订单30分钟未支付取消
            case POINTS_ONLY:
            case POINTS_AND_MONEY:
                return 60; // 积分订单60分钟未支付取消
            case KOC_EXCHANGE:
                return 1440; // KOC订单24小时未支付取消
            default:
                return 30;
        }
    }

    /**
     * 获取订单类型显示颜色（前端使用）
     */
    public String getDisplayColor() {
        switch (this) {
            case NORMAL:
                return "#1890ff";
            case POINTS_ONLY:
                return "#52c41a";
            case POINTS_AND_MONEY:
                return "#faad14";
            case KOC_EXCHANGE:
                return "#722ed1";
            default:
                return "#d9d9d9";
        }
    }

    /**
     * 获取订单类型图标（前端使用）
     */
    public String getIcon() {
        switch (this) {
            case NORMAL:
                return "shopping-cart";
            case POINTS_ONLY:
                return "gift";
            case POINTS_AND_MONEY:
                return "transaction";
            case KOC_EXCHANGE:
                return "star";
            default:
                return "question";
        }
    }

    // Getters
    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return String.format("OrderType{code=%d, name='%s', description='%s'}",
            code, name, description);
    }
}
